<%@ page import="servlets.*" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
            try {
            ResultSet rs = DBProvider.selectRecordsFromDbUserTable("SELECT ID, ORDERSTATEID, BEGINDATE, PRIORITY from Order_ order by begindate desc limit 50");
            int i = 0;
            out.print("[");

            while (rs.next()) {

                if(i > 0)
                    out.print(",");

                out.print("{");

                Integer id = Integer.parseInt(rs.getString("ID"));
                Integer orderStateId = Integer.parseInt(rs.getString("ORDERSTATEID"));
                Integer beginDate = Integer.parseInt(rs.getString("BEGINDATE"));
                Integer priority = Integer.parseInt(rs.getString("PRIORITY"));

                out.print("\"id\":\"" + id + "\", " +
                    "\"status\":\"" + orderStateId + "\", " +
                    "\"priority\":\"" + priority + "\", " +
                    "\"date\":\"" + beginDate + "\", " +
                    "\"items\":");

                out.print("[");

                ResultSet rsOrdPr = DBProvider.selectRecordsFromDbUserTable("SELECT OrderProductTable.ID, Product.Title, count(Product.Title) as amount" +
" from OrderProductTable inner join Product ON OrderProductTable.ProductId = Product.Id " +
"where OrderProductTable.OrderId=" + id + " group by Product.Title;");
                Boolean isNotFirst = false;
                while (rsOrdPr.next()) {
                    if(isNotFirst)
                        out.print(",");

                    out.print("{");

                    Integer prId = Integer.parseInt(rsOrdPr.getString("ID"));
                    String prTitle = rsOrdPr.getString("Title");
                    Integer prAmount = Integer.parseInt(rsOrdPr.getString("amount"));

                    out.print("\"id\":\"" + prId + "\", " +
                    "\"name\":\"" + prTitle + "\", " +
                    "\"qty\":\"" + prAmount + "\"");

                    out.print("}");

                    isNotFirst = true;
                }

                out.print("]");

                out.print("}");


                i++;

            }
                out.print("]");
        } catch (SQLException e) {

            System.out.println(e.getMessage());

        }


                   /*
    Order[] items = (Order[]) OrderList.getOrders();

    out.print("[");

    for(int i=0; i < items.length; i++)
    {
        if(i > 0)
            out.print(",");

        out.print("{");

        Order order = items[i];
        out.print("\"id\":\"" + order.getId() + "\", " +
                    "\"status\":\"0\", " +
                    "\"priority\":\"" + order.getPriority() + "\", " +
                    "\"date\":\"" + order.getBeginDate() + "\", " +
                    "\"items\":");

        OrderProductTable[] orderProductTableList = OrderProductTableList.getOrderProductTable();

        out.print("[");

        Boolean isNotFirst = false;
        for(int j=0; j < orderProductTableList.length; j++)
        {
            if(orderProductTableList[j].getOrders() != order.getId())
                continue;

            if(isNotFirst)
                out.print(",");

            out.print("{");

            OrderProductTable orderProductTable = orderProductTableList[j];

            out.print("\"id\":\"" + orderProductTable.getId() + "\", " +
                    "\"name\":\"");

            ProductsProvider productsProvider = new ProductsProvider();
            Product[] productsList = productsProvider.getProducts();
            for(int k = 0; k < productsList.length; k++)
            {
                if(productsList[k].getId() == orderProductTable.getProducts())
                {
                    out.print(productsList[k].getTitle());
                    break;
                }
            }

            out.print("\", " +
                    "\"qty\":\"1\"");

            out.print("}");

            isNotFirst = true;
        }

        out.print("]");

        out.print("}");
    }

    out.print("]");             */
%>